package com.eh.proxy.remote.remote.model;

import com.eh.proxy.remote.remote.base.State;
import com.eh.proxy.remote.remote.state.*;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 * Created by David on 2016/5/1.
 */
public class GumballMachine extends UnicastRemoteObject implements GumballMachineRemote {
    private String location;

    private State noQuarterState;
    private State hasQuarterState;
    private State soldState;
    private State soldOutState;
    private State winnerState;

    private State currentState = soldOutState;

    private int count; // 糖果数量

    public GumballMachine(int count, String location) throws RemoteException {
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        soldOutState = new SoldOutState(this);
        winnerState = new WinnerState(this);
        this.count = count;
        if (count > 0) {
            currentState = noQuarterState;
        }
        this.location = location;
    }

    public void insertQuarter() {
        currentState.insertQuarter();
    }

    public void ejectQuarter() {
        currentState.ejectQuarter();
    }

    public void turnCrank() {
        if (currentState.turnCrank()) {
            currentState.dispense();
        }
    }

    public void setCurrentState(State state) {
        currentState = state;
    }

    public State getCurrentState() {
        return currentState;
    }

    public void releaseBall() {
        System.out.println("A gumball comes rolling out the slot...");
        if (count != 0) {
            count--;
        }
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public State getSoldState() {
        return soldState;
    }

    public State getSoldOutState() {
        return soldOutState;
    }

    public State getWinnerState() {
        return winnerState;
    }

    public int getCount() {
        return count;
    }

    public String getLocation() {
        return location;
    }
}
